package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.query.StrategyQueryObject;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;


@RequestMapping("strategy")
@Controller
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    /**
     * 页面展示
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") StrategyQueryObject qo){
        //查询页面数据
        Page page = strategyService.query(qo);
        //分享数据到页面
        model.addAttribute("page", page);

        return "strategy/list";
    }



    @RequestMapping("/input")
    public String input(Model model, String id){
        //strategy
        model.addAttribute("strategy", strategyService.get(id));
        //catalogs 分类回显
        model.addAttribute("catalogs", strategyCatalogService.groupList());
        //themes 主题回显
        model.addAttribute("themes", strategyThemeService.list());
        return "strategy/input";
    }





    /**
     * 新增或编辑
     * @param strategy
     * @return
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy){

        strategyService.saveOrUpdate(strategy);
        return JsonResult.success();
    }



    /**
     * 删除
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        strategyService.delete(id);
        return JsonResult.success();
    }

    /**
     * 修改状态,下架
     * @param state
     * @param id
     * @return
     */
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(int state,String id){

        strategyService.changeState(state,id);
        return JsonResult.success();
    }

}
